package server
{
	import com.smartfoxserver.v2.*;
	import com.smartfoxserver.v2.core.*;
	import com.smartfoxserver.v2.entities.*;
	import com.smartfoxserver.v2.entities.data.*;
	import com.smartfoxserver.v2.entities.variables.*;
	import com.smartfoxserver.v2.requests.*;
	
	public class Server
	{
		public var smartFox: SmartFox;
		public var game:Game; 
			
			public function Server(g:Game)
			{           
				this.game = g;
				this.game.printMsg("VENGO A FLOTAR");
				init();
			}
			
			public function init():void//refDoc:*
			{           
				// Get the references to the Document Class
				//refDocument = refDoc;   
				
				// Init stuff and SmartFox listeners
				initSmartFox();
			}
			
			private function initSmartFox():void
			{
				this.game.printMsg("VENGO A FLOTAR 2");
				//smartFox = connector_mc.connection;
				smartFox = new SmartFox();
				smartFox.connect("192.168.2.150", 9933);
				smartFox.addEventListener(SFSEvent.CONNECTION, onConection);
				smartFox.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoined);
			}
			
			public function onConection(success:SFSEvent):void
			{
				this.game.printMsg("ON CONNECTION...");
				if(success)
				{
					this.game.printMsg("SUCCESS!");
					initRoom();
					initListeners();
				}
			}
			
			private function onRoomJoined(evt:SFSEvent):void
			{
				this.game.printMsg("ON ROOM JOINED...");
				sendMsg("HOLA GAME JAM!");
			}
			
			private function initRoom():void
			{
				//var roomVar:SFSRoomVariable = new SFSRoomVariable("room", "1");
				//roomVar.isPersistent        = true;// Add room variable to an array
				//var roomVars:Array          = new Array();
				//roomVars.push(roomVar);
				// Create room settings
				/*var roomSettings:RoomSettings = new RoomSettings("room1"); 
				//roomSettings.password           = password_ti.text;
				roomSettings.maxUsers           = 2;
				roomSettings.maxSpectators      = 0;
				roomSettings.isGame           = true;
				roomSettings.variables        = roomVars;*/
				// Create new game room with above parameters and join it
				//smartFox.send(new CreateRoomRequest(roomSettings, true, smartFox.lastJoinedRoom));
				smartFox.send(new JoinRoomRequest("room1"));
			}
			
			private function initListeners():void
			{
				
				// Activate SmartFox listeners
				//refDocument.smartFox.addEventListener(SFSEvent.CONNECTION_LOST      , onConnectionLost);
				smartFox.addEventListener(SFSEvent.EXTENSION_RESPONSE, onGameExtensionResponse);
				
				// Tell the server that player is ready to start the game
				//refDocument.smartFox.send(new ExtensionRequest("ready", new SFSObject()));
			}
			
			
			private function onGameExtensionResponse(evt:SFSEvent):void
			{
				
				var extParams:SFSObject = evt.params.params;
				
				switch(evt.params.cmd)
				{
					
					case "msg"   : handleEcho(extParams.getUtfString("msg"));
						break;
				}
			}
			
			/* Send my move to the opponent
			*/
				private function sendMsg(msgText:String):void
				{
					var paramsObj:SFSObject = new SFSObject();
					paramsObj.putUtfString("msg", msgText);
					
					smartFox.send(new ExtensionRequest("msg", paramsObj));
				}
			
			
			private function handleEcho(msg:String):void
			{
				this.game.printMsg("ECHO ECHO ECHO " + msg);
			}
		}
}